package org.example.video.dao;

import org.example.video.pojo.Video;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

public interface VideoRepository extends JpaRepository<Video, UUID> {
    List<Video> findByStatus(Video.Status status);

    /**
     * 按标题模糊查询，如果标题为空则查询所有
     */
    @Query("SELECT v FROM Video v WHERE (:title IS NULL OR v.title LIKE %:title%)")
    Page<Video> findByTitleContaining(@Param("title") String title, Pageable pageable);

    @Modifying
    @Transactional
    @Query("update Video v set v.status = 'READY', v.playlistKey = ?2 where v.id = ?1")
    void markReady(UUID id, String playlistKey);

    @Modifying
    @Transactional
    @Query("update Video v set v.status = 'ERROR' where v.id = ?1")
    void markError(UUID id, String errorMsg);
}